import { Vue, Component } from 'vue-property-decorator'
import { DIALOGUE, ImClient } from '@/api/socket/imClient';
declare module 'vue/types/vue' {
  interface Vue {
    initEvents(eventList: Array<string>): void
    sendMessage(type: number, msgData?: any): void
  }
}

declare const $Vue: Vue;
/**
* mixins
*/
@Component
export default class MessageMixin extends Vue {

  initEvents(eventList) {
    eventList.forEach((item: string) => {
      $Vue.$off(item);
      $Vue.$on(item, this[item]);
    });
  }

  sendMessage(type: number, msgData?: any) {
    msgData = msgData || {};
    let typeStr = type.toString();
    if (typeStr.length === 5) {
      type = Number(typeStr.substring(0, 4));
      msgData.paramType = Number(typeStr.substring(4, 5));
    }
    this.$socket.emit(DIALOGUE, ImClient.Message(type, msgData));
  }
}